package com.party.mobile.web.controller.sign;

import com.party.authorization.annotation.Authorization;
import com.party.common.paging.Page;
import com.party.common.utils.PartyCode;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.core.model.member.Member;
import com.party.core.model.sign.SignApply;
import com.party.core.model.sign.SignProject;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.sign.ISignApplyService;
import com.party.core.service.sign.ISignProjectService;
import com.party.mobile.biz.currentUser.CurrentUserBizService;
import com.party.mobile.biz.sign.SignProjectBizService;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.login.output.CurrentUser;
import com.party.mobile.web.dto.sign.output.ApplyDetailOutput;
import com.party.mobile.web.dto.sign.output.SignProjectListOutput;
import com.party.mobile.web.dto.sign.output.SignProjectOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 签到项目控制器
 * Created by wei.li
 *
 * @date 2017/6/8 0008
 * @time 17:09
 */

@Controller
@RequestMapping(value = "sign/project")
public class SignProjectController {

    @Autowired
    private SignProjectBizService signProjectBizService;

    @Autowired
    private CurrentUserBizService currentUserBizService;

    @Autowired
    private ISignApplyService signApplyService;

    @Autowired
    private IActivityService activityService;

    @Autowired
    private ISignProjectService signProjectService;
    @Autowired
    private IMemberService memberService;

    Logger logger = LoggerFactory.getLogger(getClass());


    /**
     * 获取签到项目详情
     *
     * @param id      项目编号
     * @param request 请求参数
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "detail")
    public AjaxResult detail(String id, HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
        SignProjectOutput signProjectOutput = null;
        try {
            signProjectOutput = signProjectBizService.get(id, currentUser);
        } catch (BusinessException be) {
            ajaxResult.setErrorCode(be.getCode());
            ajaxResult.setDescription(be.getMessage());
            return ajaxResult;
        } catch (Exception e) {
            ajaxResult.setDescription(e.getMessage());
            e.printStackTrace();
            return ajaxResult;
        }
        ajaxResult.setSuccess(true);
        ajaxResult.setData(signProjectOutput);
        return ajaxResult;
    }

    /**
     * 转发页面获取签到项目详情
     *
     * @param
     * @param
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "detailRedirect")
    public AjaxResult detailRedirect(String projectId, String memberIdt) {
        AjaxResult ajaxResult = new AjaxResult();
        CurrentUser currentUser = new CurrentUser();
        currentUser.setId(memberIdt);
        SignProjectOutput signProjectOutput = null;
        try {
            signProjectOutput = signProjectBizService.get(projectId, currentUser);
        } catch (BusinessException be) {
            ajaxResult.setErrorCode(be.getCode());
            ajaxResult.setDescription(be.getMessage());
            return ajaxResult;
        } catch (Exception e) {
            ajaxResult.setDescription(e.getMessage());
            e.printStackTrace();
            return ajaxResult;
        }
        ajaxResult.setSuccess(true);
        ajaxResult.setData(signProjectOutput);
        return ajaxResult;
    }

    /**
     * 获取项目规则内容
     *
     * @param id 项目id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getRuleContent")
    public AjaxResult getRuleContent(String id) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "项目id不能为空");
        }
        try {
            String ruleContent = signProjectBizService.getRuleContent(id);
            return AjaxResult.success((Object) ruleContent);
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 转发页面获取项目规则内容
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getRuleContentRedirect")
    public AjaxResult getRuleContentRedirect(String projectId, String memberId) {
        if (StringUtils.isEmpty(projectId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "项目id不能为空");
        }
        try {
            String ruleContent = signProjectBizService.getRuleContent(projectId);
            return AjaxResult.success((Object) ruleContent);
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 一周运动数据
     *
     * @param projectId 项目id
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping("oneWeekSignData")
    public AjaxResult oneWeekSignData(String projectId, HttpServletRequest request) {
        if (StringUtils.isEmpty(projectId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "项目id不能为空");
        }
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            List<Map<String, Object>> weekSignData = signProjectBizService.getOneWeekSignData(projectId, currentUser.getId());
            return AjaxResult.success(weekSignData);
        } catch (Exception e) {
            logger.error("获取一周运动数据异常", e);
            return AjaxResult.error();
        }
    }

    /**
     * 转发页面一周运动数据
     *
     * @param
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping("oneWeekSignDataRedirect")
    public AjaxResult oneWeekSignDataRedirect(String projectId, String memberId) {
        if (StringUtils.isEmpty(projectId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "项目id不能为空");
        }
        try {
            List<Map<String, Object>> weekSignData = signProjectBizService.getOneWeekSignData(projectId, memberId);
            return AjaxResult.success(weekSignData);
        } catch (Exception e) {
            logger.error("获取一周运动数据异常", e);
            return AjaxResult.error();
        }
    }

    /**
     * 报名详情页面
     *
     * @param id 项目编号
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "apply/detail")
    public AjaxResult applyDetail(String id) {
        AjaxResult ajaxResult = new AjaxResult();
        ApplyDetailOutput applyDetailOutput = signProjectBizService.getApplyDetail(id);
        ajaxResult.setData(applyDetailOutput);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    /**
     * 我参加的打卡列表
     *
     * @param request
     * @param page    分页参数
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "myJoinList", method = RequestMethod.GET)
    public AjaxResult myJoinList(HttpServletRequest request, Page page) {
        try {
            String memberId = currentUserBizService.getCurrentUser(request).getId();
            List<SignProjectListOutput> projectList = signProjectBizService.getMyJoinList(memberId, page);
            return AjaxResult.success(projectList, page);
        } catch (Exception e) {
            logger.error("我参加的打卡列表异常", e);
            return AjaxResult.error();
        }
    }

    /**
     * 我参加最新打卡项目
     *
     * @param request
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "myJoinProject", method = RequestMethod.GET)
    public AjaxResult myJoinProject(HttpServletRequest request, Page page) {
        try {
            String memberId = currentUserBizService.getCurrentUser(request).getId();
            // String memberId = "9fd95ea6b8004fbeb6c11d83275c9b4b";
            List<SignProjectListOutput> projectList = signProjectBizService.myJoinProject(memberId);
            return AjaxResult.success(projectList);
        } catch (Exception e) {
            logger.error("我参加的打卡列表异常", e);
            return AjaxResult.error();
        }
    }

    /**
     * 转发页面我参加的打卡项目
     *
     * @param
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "myJoinProjectRedirect", method = RequestMethod.GET)
    public AjaxResult myJoinProjectRedirect(String memberId) {
        try {
            List<SignProjectListOutput> projectList = signProjectBizService.myJoinProject(memberId);
            return AjaxResult.success(projectList);
        } catch (Exception e) {
            logger.error("我参加的打卡列表异常", e);
            return AjaxResult.error();
        }
    }


    /**
     * 获取签到项目详情
     *
     * @param id      项目编号
     * @param request 请求参数
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "detailXcx")
    public AjaxResult detailXcx(String id, HttpServletRequest request) {
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            SignProjectOutput signProjectOutput = signProjectBizService.getDetailXcx(id, currentUser);
            return AjaxResult.success(signProjectOutput);
        } catch (BusinessException be) {
            return AjaxResult.error(be.getCode(), be.getMessage());
        } catch (Exception e) {
            logger.error("详情异常", e);
            return AjaxResult.error();
        }
    }

    /**
     * 获取签到项目详情
     *
     * @param projectId
     * @param memberId
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "detailXcxRedirect")
    public AjaxResult detailXcxRedirect(String projectId, String memberId) {
        try {
            CurrentUser currentUser = new CurrentUser();
            currentUser.setId(memberId);
            SignProjectOutput signProjectOutput = signProjectBizService.getDetailXcx(projectId, currentUser);
            return AjaxResult.success(signProjectOutput);
        } catch (BusinessException be) {
            return AjaxResult.error(be.getCode(), be.getMessage());
        } catch (Exception e) {
            logger.error("详情异常", e);
            return AjaxResult.error();
        }
    }


    /**
     * 项目列表
     *
     * @param page
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping(value = "list")
    public AjaxResult list(Page page, HttpServletRequest request) {
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            List<SignProjectListOutput> projectList = signProjectBizService.getList(page, currentUser.getId());
            return AjaxResult.success(projectList, page);
        } catch (Exception e) {
            logger.error("项目列表异常", e);
            return AjaxResult.error();
        }
    }


    /**
     * 获取项目分享背景图片
     *
     * @param projectId 项目id
     * @return
     */
    @ResponseBody
    @RequestMapping("getShareBackground")
    public AjaxResult getShareBackground(String projectId) {
        if (StringUtils.isEmpty(projectId)) {
            return AjaxResult.error(PartyCode.PARAMETER_ILLEGAL, "项目id不能为空");
        }

        try {
            String[] picList = signProjectBizService.getShareBackground(projectId);
            return AjaxResult.success(picList);
        } catch (Exception e) {
            logger.error("获取项目分享背景图片异常：projectId-{}", projectId, e);
            return AjaxResult.error();
        }
    }

    /**
     * 根据报名id查询转发者者信息
     *
     * @param memberId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getMember")
    public AjaxResult getMember( String memberId) {
        try {
            AjaxResult ajaxResult = new AjaxResult();
                Member member = memberService.get(memberId);
                ajaxResult.setData(member);
                ajaxResult.setSuccess(true);

            return ajaxResult;
        } catch (Exception e) {
            logger.error("项目列表异常", e);
            return AjaxResult.error();
        }
    }

    /**
     * 根据查询发布者id
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getMemberId")
    public AjaxResult getMemberId(String projectId) {
        try {
            AjaxResult ajaxResult = new AjaxResult();
            SignProject signProject = signProjectService.get(projectId);
            if (null != signProject) {
                String memberId = signProject.getCreateBy();
                ajaxResult.setData(memberId);
                ajaxResult.setSuccess(true);
            } else {
                return AjaxResult.error(100, "打卡项目不存在");
            }

            return ajaxResult;
        } catch (Exception e) {
            logger.error("项目列表异常", e);
            return AjaxResult.error();
        }
    }

    @ResponseBody
    @Authorization
    @RequestMapping(value = "getStatusRedirect")
    public AjaxResult getCurrentUser(String projectId,  HttpServletRequest request) {
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            AjaxResult ajaxResult = new AjaxResult();
                String memberId = currentUser.getId();
                SignApply signApply = signApplyService.get(memberId, projectId);
                ajaxResult.setData(signApply);
                ajaxResult.setSuccess(true);
                if (null == logger) {
                    return AjaxResult.error(100, "报名项目不存在");
                }
            return ajaxResult;
        } catch (Exception e) {
            logger.error("获取用户信息异常", e);
            return AjaxResult.error();
        }
    }

    /**
     * 机构的项目列表
     *
     * @param request request
     * @param page    分页
     * @return
     */
    @ResponseBody
    @Authorization
    @RequestMapping("orgProjectList")
    public AjaxResult orgProjectList(HttpServletRequest request, Page page) {
        try {
            CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
            List<SignProjectListOutput> projectList = signProjectBizService.getOrgProjectList(page, currentUser.getId());
            return AjaxResult.success(projectList, page);
        } catch (Exception e) {
            logger.error("获取打卡列表数据异常", e);
            return AjaxResult.error(100, "获取打卡列表数据异常");
        }
    }
}
